// pages/killPrice/details.js
var utils = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    isHide: true,
    isMine: true,
    isCoupn: false,
    isHtml: false,
    ishise: false,
    coupon: "领劵"
  },
  saveCanvas: function() {
    var that = this;
    var url = 'https://xinong.qidian2018.com/index.php?share_id=1&type=2&id=' + that.data.id + '&member_id=' + that.data.localInfo.id;
    url += that.data.order_id ? '&order_id=' + that.data.order_id : '';
    console.log(url);
    that.setData({
      qrcodeValue: url,
    }, function() {
      var canvas = that.selectComponent('#goods-share');
      canvas.saveImg();
    })
  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      //砍价的id
      id: options.id,
      order_id: options.order_id ? options.order_id : '',
      number: wx.getStorageSync("number"),
      fineData: wx.getStorageSync('fineData'),
    });
    that.personalInfo();
    that.categorylist(options.id);
    that.coupnlistTap(options.id)
  },
  //分享弹窗
  onshareTap: function (e) {
    var that = this;
    var url = 'https://xinong.qidian2018.com/index.php?share_id=1&type=2&id=' + that.data.id + '&member_id=' + that.data.localInfo.id;
    url += that.data.order_id ? '&order_id=' + that.data.order_id : '';
    that.setData({
      qrcodeValue: url,
      ishise: true
    })
    console.log(this.data.qrcodeValue)
  },
  buttonOrderCalse: function (e) {
    this.setData({
      ishise: false
    })
  },
  //领取优惠券
  onCouponTap: function () {
    this.setData({
      isCoupn: true,
      isHtml: false,
    })
  },
  //领取优惠券  关闭
  onCouponClaseTap: function () {
    this.setData({
      isCoupn: false,
      isHtml: false,
    })
  },

  //参数
  onHtmlTap: function () {
    this.setData({
      isHtml: true,
      isCoupn: false
    })
  },
  //参数
  onHtmlClaseTap: function () {
    this.setData({
      isHtml: false,
      isCoupn: false
    })
  },
  //优惠卷使用
  onIndexTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    // 收取优惠券
    utils.http("/api/v1/coupon/get_coupon", {
      coupon_id: that.data.couponList[index].id
    }, function (res) {
      if (res.state == 1) {
        that.setData({
          isCoupn: false,
          coupon: that.data.couponList[index].money
        })
      } else {
        utils.message(res.datas)
      }
    })

  },
  //优惠券列表
  coupnlistTap: function (id) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/goods/coupon_list", {
      goods_id: id
    }, function (res) {
      utils.hideLoading();
      that.setData({
        couponList: res.datas,
      });
    })
  },
  //当前商品的砍价的详情
  bargain_order_details: function(order_id = '') {
    var that = this;
    if (!order_id) {
      var url = '/api/v1/bargaingoods/bargain_my_order';
      var param = {
        bargain_id: that.data.id,
        goods_id: that.data.dataDetail.goods_id,
        spec_id: that.data.dataDetail.spec_id,
      };
    } else {
      var url = '/api/v1/bargaingoods/bargain_order';
      var param = {
        order_id: order_id
      };
    }
    utils.showLoading();
    utils.http(url, param, function(res) {
      utils.hideLoading();
      if (!res.datas) return false;
      that.setData({
        order_id: res.datas.id,
        bargain_order: res.datas,
        info: res.datas.member_info,
        isMine: that.data.localInfo.id == res.datas.member_id ? true : false
      }, function() {
        that.formatTime(res.datas.finish_time * 1000);
        that.bargain_log()
      })
    })
  },
  //发起砍价订单（获取砍价订单id）
  add_bargain_order: function(callback = '') {
    var that = this;
    if (that.data.order_id) {
      that.make_bargain();
    }
    utils.showLoading();
    utils.http("/api/v1/bargaingoods/add_bargain_order", {
      bargain_id: that.data.dataDetail.id,
      goods_id: that.data.dataDetail.goods_id,
      spec_id: that.data.dataDetail.spec_id,
    }, function(res) {
      if (res.state == 1) {
        that.setData({
          order_id: res.datas.id
        }, function() {
          typeof callback == 'function' ? callback() : ''
        });
      } else {
        wx.showToast({
          title: res.datas,
          icon: 'none'
        })
      }
      utils.hideLoading();
    })
  },
  //砍价记录
  bargain_log: function() {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/bargaingoods/bargain_log", {
      bargain_order_id: that.data.order_id
    }, function(res) {
      if (res.state == 1) {
        that.setData({
          bargain_log: res.datas
        });
      } else {
        wx.showToast({
          title: res.datas,
          icon: 'none'
        })
      }
      utils.hideLoading();
    })
  },
  //商品详情
  categorylist: function(id) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/bargaingoods/goods_detail", {
      id: id
    }, function(res) {
      utils.hideLoading();
      that.setData({
        dataDetail: res.datas,
      }, function() {
        WxParse.wxParse('article', 'html', res.datas.goods_info.content, that, 5);
		  WxParse.wxParse('article1', 'html', res.datas.goods_info.goods_param, that, 5);
        //当前商品的砍价的详情
        that.bargain_order_details(that.data.order_id);
      });
    })
  },
  //砍价动作,是兄弟就来砍我~!
  make_bargain: function() {
    var that = this;
    if (that.data.order_id) {
      that.make_bargain_fun()
    } else {
      that.add_bargain_order(function() {
        that.make_bargain_fun()
      });
    }
  },
  //我要砍价
  make_bargain_fun: function() {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/bargaingoods/make_bargain", {
      order_id: that.data.order_id
    }, function(res) {
      utils.hideLoading();
      if (res.state != 1) {
        wx.showToast({
          title: res.datas,
          icon: "none"
        });
        return false;
      }
      that.setData({
        isHide: false,
        kill_price: res.datas,
      }, function() {
        that.bargain_order_details(that.data.order_id);
        that.bargain_log();
      });
    })
  },
  formatTime: function(time) {
    var that = this;
    //获取当前时间
    var nowTime = Date.now();
    // var nowTime=new Date().getTime();
    //获取时间差
    var timediff = Math.round((time - nowTime) / 1000);
    if (timediff < 0) {
      return false;
    }
    //获取还剩多少天
    var day = parseInt(timediff / 3600 / 24);
    //获取还剩多少小时
    var hour = parseInt(timediff / 3600 % 24);
    //获取还剩多少分钟
    var minute = parseInt(timediff / 60 % 60);
    //获取还剩多少秒
    var second = timediff % 60;
    //输出还剩多少时间
    that.setData({
      format_time: day + "天" + hour + "时" + minute + "分" + second + "秒"
    }, function() {
      setTimeout(function() {
        that.formatTime(time)
      }, 1000);
    })
    if (timediff == 0) {
      return;
    }
  },
  //浏览图片
  imgTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: e.currentTarget.dataset.http, // 当前显示图片的http链接
      urls: that.data.comments[index].image // 需要预览的图片http链接列表
    })
  },
  //跳转商品评价更多页面
  onEvaluateTap: function(e) {
    var audioCtx = wx.createVideoContext('myAudio');
    audioCtx.pause();
    wx.navigateTo({
      url: '../commoditydetails/moreevaluatelist/moreevaluatelist?id=' + this.data.id,
    })
  },
  //立即购买
  buyNowTap: function(e) {
    var that = this;
    if (!that.data.order_id) {
      // wx.showToast({
      //   title: '请先发起砍价~',        
      //   icon: 'none'
      // })
      wx.showModal({
        title: '请先发起砍价',
        success: function(res) {
          if (res.confirm == true) {
            that.make_bargain();
          }
        }
      })
      return null;
    } else {
      var bargainGoodsItem = that.data.dataDetail;
      var goodsList = [{
        spec_id: bargainGoodsItem.spec_id,
        number: 1,
        selectStatus: true,
        name: bargainGoodsItem.goods_info.name,
        image: bargainGoodsItem.goods_info.image,
        subtitle: bargainGoodsItem.goods_info.subtitle,
        spec_price: that.data.bargain_order.total_price,
        spec_vip_price: that.data.bargain_order.total_price,
        spec_stock: bargainGoodsItem.spec_info.spec_stock,
        spec_name: bargainGoodsItem.spec_info.spec_name
      }];
      console.log(goodsList)
      // return;
      wx.setStorageSync('bargainGoods', goodsList);
      var url = '/pages/confirmorder/confirmorder?type=2&order_id=' + that.data.order_id;
      wx.navigateTo({
        url
      });
      return;
      wx.navigateTo({
        url: '/pages/killPrice/orderPay?order_id=' + that.data.order_id,
      })
    }

  },
  //我的个人信息
  personalInfo: function(e) {
    var that = this;
    utils.http("/api/v1/member/index", {}, function(res) {
      that.setData({
        localInfo: res.datas,
      })
    })
  },
  //判断是不是当前视频播放状态
  bindtransitionTap: function(e) {
    var audioCtx = wx.createVideoContext('myAudio');
    console.log(audioCtx)
    if (e.detail.current != 0) {
      audioCtx.pause()
    } else {
      audioCtx.play()
    }
  },
  // 点击cover播放，其它视频结束 
  videoPlay: function(e) {
    var audioCtx = wx.createVideoContext('myAudio');
    audioCtx.pause();
    var _index = e.currentTarget.id
    console.log(_index)
    this.setData({
      _index: _index
    })
    //停止正在播放的视频 
    var videoContextPrev = wx.createVideoContext(this.data._index)
    videoContextPrev.stop();
    setTimeout(function() { //将点击视频进行播放   
      var videoContext = wx.createVideoContext(_index)
      videoContext.play();
    }, 500)
  },
  //关闭提交成功
  onCoseTap: function(e) {
    var that = this;
    that.setData({
      isHide: true
    })
  },
  onShareAppMessage: function() {
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var url = currentPage.route //当前页面url
    var options = currentPage.options //如果要获取url中所带的参数可以查看options
    var param = '';
    for (var key in options) {
      param += '&' + key + '=' + options[key];
    }
    var path = '/' + url + '?id=' + this.data.id;
    path = path + '&order_id=' + this.data.order_id;
	  console.log(this.data.dataDetail,"slkdjfklsdjfklsj")
	  return utils.share(path, this.data.dataDetail.goods_info.name)
  }
})